package no.sachmuld.methodcombination.examples.performance.adapter;

import no.sachmuld.methodcombination.api.BehaviourChain;
import no.sachmuld.methodcombination.api.MethodInvocation;
import no.sachmuld.methodcombination.api.adapter.Standard;
import no.sachmuld.methodcombination.examples.performance.FooBehaviour;
import no.sachmuld.methodcombination.examples.performance.Step;

import java.util.List;

/**
 *
 */
public abstract class Foo implements no.sachmuld.methodcombination.examples.performance.Foo {

    private BehaviourChain<FooBehaviour> chain = new BehaviourChain<>();
    protected List<Step> trace;

    public void setTrace(List<Step> trace) {
        this.trace = trace;
    }

    @Override
    final public void bar(final int i) {
        new Standard<FooBehaviour, Void>(chain) {

            @Override
            protected Void executeAround(FooBehaviour behaviour, MethodInvocation<Void> methodInvocation) {
                behaviour.aroundBar(i, methodInvocation);
                return null;
            }

            @Override
            protected void executeBefore(FooBehaviour behaviour) {
                behaviour.beforeBar(i);
            }

            @Override
            protected Void executePrimary(FooBehaviour behaviour, MethodInvocation<Void> methodInvocation) {
                behaviour.bar(i, methodInvocation);
                return null;
            }

            @Override
            protected void executeAfter(FooBehaviour behaviour) {
                behaviour.afterBar(i);
            }
        }.execute();
    }

    final protected Foo with(FooBehaviour... fooBehaviours) {
        chain.addAll(fooBehaviours);
        return this;
    }


}
